﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FreesenseSubAIS.UI
{
    public partial class FormPic : Form
    {
        Bitmap btt;

        public FormPic()
        {
            InitializeComponent();
            imageBox1.CopyMode = true;
        }
        public  Bitmap bt
        {
            get
            {
                return btt;
            }
            set
            {
                if (btt != null)
                {
                    btt.Dispose();
                }
               btt  = value;
               DisPic(btt);
               //DisImageBox(btt);
            }
        }

        private void DisImageBox(Bitmap btt)
        {
            double wscale = (double)imageBox1.Width / (double)btt.Width;
            double hscal = (double)imageBox1.Height / (double)btt.Height;

            imageBox1.SetZoomScale(wscale > hscal ? hscal : wscale, new System.Drawing.Point(0, 0));
             imageBox1.Invoke(new Action(() =>
            {
                imageBox1.Image = btt;
                imageBox1.Refresh();
            }));
        }

        private void DisPic(Bitmap btt)
        {
            if (pictureBox1.IsDisposed )
                return;
            pictureBox1.Invoke(new Action(() =>
            {
                pictureBox1.Image = bt;
                pictureBox1.Refresh();
            }));
        }
        


        private void tr_Tick(object sender, EventArgs e)
        {
            try
            {
                if (pictureBox1.InvokeRequired)
                    return;
                pictureBox1.Invoke(new Action(() =>
                {
                    pictureBox1.Image = bt;
                    pictureBox1.Refresh();
                }));
            }
            catch { }
        }

        private void FormPic_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (bt != null)
            {
                bt.Dispose();
            }
        }

    }
}
